package com.ldc.wandroid.ui.activitys;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.views.WxSharedDialog;
import com.ldc.wandroid.R;
import com.ldc.wandroid.databinding.ActivityShowArticleWebBinding;
import com.ldc.wandroid.viewmodel.ShowArticleWebViewModel;
import com.squareup.picasso.Picasso;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class ShowArticleWebActivity extends BaseMvvmActivity<ActivityShowArticleWebBinding, ShowArticleWebViewModel> {


    private String strTitle = "";
    private String strUrl = "";
    //
    private AgentWeb agentWeb;
    //

    public static void actionStart(Activity activity, String title, String url) {
        Intent intent = new Intent(activity, ShowArticleWebActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    @Override
    protected void onPause() {
        if (null != agentWeb) {
            agentWeb.getWebLifeCycle().onPause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (null != agentWeb) {
            agentWeb.getWebLifeCycle().onResume();
        }
        super.onResume();
    }

    @Override
    protected ShowArticleWebViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, ShowArticleWebViewModel.class);
    }

    @Override
    protected void onDestroy() {
        if (null != agentWeb) {
            agentWeb.getWebLifeCycle().onDestroy();
        }
        super.onDestroy();
    }

    @Override
    protected ActivityShowArticleWebBinding initViewBinding() {
        return ActivityShowArticleWebBinding.inflate(getLayoutInflater());
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        strTitle = getIntent().getStringExtra("title");
        strUrl = getIntent().getStringExtra("title");
        viewBinding.topBar.tvTitle.setText(String.format("%s", strTitle));
        Picasso.get().load(R.drawable.icon_shared_article).into(viewBinding.topBar.ivMore);
        viewBinding.topBar.lineBack.setOnClickListener(v -> finish());
        viewBinding.topBar.lineMore.setOnClickListener((v) -> {
            showWxDialog();
        });
        // 请求权限
        AndPermission.with(this)
                .runtime()
                .permission(Permission.READ_EXTERNAL_STORAGE,
                        Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_NETWORK_STATE)
                .start();
        if (TextUtils.isEmpty(strUrl)) {
            ToastUtils.showShort("无效地址~~");
            finish();
        } else {
            initWeb2();
        }
        viewBinding.stateView.showContentView();
    }


    private void initWeb2() {
        try {
            agentWeb = AgentWeb.with(this)
                    .setAgentWebParent(viewBinding.webView, new LinearLayout.LayoutParams(-1, -1))
                    .useDefaultIndicator()
                    .setSecurityType(AgentWeb.SecurityType.DEFAULT_CHECK)
                    .createAgentWeb()
                    .ready()
                    .go(strUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @see "显示分享"
     */
    private void showWxDialog() {
        WxSharedDialog dialog = new WxSharedDialog();
        dialog.setListener((view, viewId) -> {
            final String baseSharedDesc = String.format("来自安卓玩客户端\t%s", "");
            switch (viewId) {
                case R.id.rl_wx_session:
                    //12
                    break;
                case R.id.rl_wx_timeline:
                    //12
                    break;
                default:
                    break;
            }
        });
        dialog.show(getSupportFragmentManager(), "wxDialog");
    }
}
